package com.example.mylibrarybase.yumengmeng24.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class InterceptorFrame extends FrameLayout {

	public static final int ORIENTATION_UP = 0x1;
	public static final int ORIENTATION_DOWN = 0x2;
	public static final int ORIENTATION_LEFT = 0x4;
	public static final int ORIENTATION_RIGHT = 0x8;
	public static final int ORIENTATION_ALL = 0x10;

	public InterceptorFrame(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public InterceptorFrame(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public InterceptorFrame(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	private HashMap<View, Integer> mHashMap = new HashMap<View, Integer>();

	// 添加一个拦截的view
	public void addInterceptorView(View view, int orientation) {
		mHashMap.put(view, orientation);
	}

	public void removeInterceptorView(View view) {
		mHashMap.remove(view);
	}

	private View mMotionTarget = null;
	private float downX;
	private float downY;

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			mMotionTarget = findTarget(ev, ORIENTATION_ALL);
			downX = ev.getX();
			downY = ev.getY();
		}

		if (ev.getAction() == MotionEvent.ACTION_MOVE) {
			float currentX = ev.getX();
			float currentY = ev.getY();

			if (Math.abs(currentX - downX) > Math.abs(currentY - downY)) {

				if (currentX - downX > 0) {
					mMotionTarget = findTarget(ev, ORIENTATION_RIGHT);
				} else {
					mMotionTarget = findTarget(ev, ORIENTATION_LEFT);
				}

			} else {
				if (currentY - downY > 0) {
					mMotionTarget = findTarget(ev, ORIENTATION_DOWN);
				} else {
					mMotionTarget = findTarget(ev, ORIENTATION_UP);
				}

			}

		}

		return super.dispatchTouchEvent(ev);
	}

	/**
	 * 寻找轮播图
	 * 
	 * @param ev
	 *            MotionEvent
	 * @param orientationAll
	 *            手势拦截的方向
	 * @return
	 */
	private View findTarget(MotionEvent ev, int orientationAll) {
		Set<Entry<View, Integer>> entrySet = mHashMap.entrySet();
		for (Entry<View, Integer> entry : entrySet) {
			View view = entry.getKey();
			Integer orientationValue = entry.getValue();
			if ((orientationValue & orientationAll) != 0
					&& isViewInEvent(ev, view) && view.dispatchTouchEvent(ev)) {
				return view;
			}
		}
		return null;
	}

	private boolean isViewInEvent(MotionEvent ev, View view) {

		int x = 0;

		int y = 0;

		ViewParent parent = view.getParent();

		while (true) {
			if (parent != null && parent instanceof View) {

				x += ev.getX();
				y += ev.getY();

				if (this.equals(parent)) {
					break;
				}
			}
			parent = parent.getParent();

			Rect frame = new Rect();
			frame.left = x;
			frame.top = y;
			frame.right = x + view.getMeasuredWidth();
			frame.bottom = y + view.getMeasuredHeight();
			return frame.contains((int) ev.getX(), (int) ev.getY());
		}
		return false;

	}
}
